﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Cosmetic.DataEntity;

namespace Cosmetic.Logic
{
    public class PointManager:LogicBase
    {
        public List<ProductInfo> SearchProductInfo(string txtContent)
        {
            var result = (from theProduct in m_CosmeticDC.ProductInfos
                          where theProduct.Name.Contains(txtContent)
                          select theProduct).ToList();
            return result;
        }


        public IEnumerable GetPointProducts()
        {
            var result = from theProduct in m_CosmeticDC.ProductInfos
                         join thePoint in m_CosmeticDC.PointProducts
                         on theProduct.ProductId equals thePoint.ProductId
                         orderby thePoint.Sequence ascending
                         select new
                         {
                             Id = thePoint.Id,
                             Name = theProduct.Name,
                             ProductId = theProduct.ProductId,
                             CashAmount = thePoint.CashAmount,
                             Sequence = thePoint.Sequence,
                             PictureUrl = theProduct.PictureUrl,
                             Views = theProduct.Views,
                             网络零售价 = theProduct.网络零售价
                         };
            return result;
        }

        public IEnumerable GetPointProductWithoutCash()
        {
            var result = from theProduct in m_CosmeticDC.ProductInfos
                         join thePoint in m_CosmeticDC.PointProducts
                         on theProduct.ProductId equals thePoint.ProductId
                         orderby thePoint.Sequence ascending
                         select new
                         {
                             Id = thePoint.Id,
                             Name = theProduct.Name,
                             ProductId = theProduct.ProductId,
                             CashAmount = thePoint.CashAmount,
                             Sequence = thePoint.Sequence,
                             PictureUrl = theProduct.PictureUrl,
                             Views = theProduct.Views,
                             网络零售价 = theProduct.网络零售价
                         };
            return result.Where(c=>c.CashAmount==0).ToList();
        }

        public IEnumerable GetPointProductWithCash()
        {
            var result = from theProduct in m_CosmeticDC.ProductInfos
                         join thePoint in m_CosmeticDC.PointProducts
                         on theProduct.ProductId equals thePoint.ProductId
                         orderby thePoint.Sequence ascending
                         select new
                         {
                             Id = thePoint.Id,
                             Name = theProduct.Name,
                             ProductId = theProduct.ProductId,
                             CashAmount = thePoint.CashAmount,
                             Sequence = thePoint.Sequence,
                             PictureUrl = theProduct.PictureUrl,
                             Views = theProduct.Views,
                             网络零售价 = theProduct.网络零售价
                         };
            return result.Where(c=>c.CashAmount!=0).ToList();
        }

        public void UpdatePointProduct(int Id,int Sequence,decimal CashAmount)
        {
            var result = (from thePoint in m_CosmeticDC.PointProducts
                          where thePoint.Id == Id
                          select thePoint).Single();
            result.Sequence = Sequence;
            result.CashAmount = CashAmount;

            m_CosmeticDC.SubmitChanges();
        }

        public void InsertPointProduct(Guid ProductId, decimal CashAmount, int Sequence)
        {
            PointProduct thePoint = new PointProduct();
            thePoint.ProductId = ProductId;
            thePoint.CashAmount = CashAmount;
            thePoint.Sequence = Sequence;
            thePoint.CreateTime = DateTime.Now;
            
            m_CosmeticDC.PointProducts.InsertOnSubmit(thePoint);

            m_CosmeticDC.SubmitChanges();
        }

        public void DeletePointProduct(int Id)
        {
            var result = (from thePoint in m_CosmeticDC.PointProducts
                          where thePoint.Id == Id
                          select thePoint).Single();
            m_CosmeticDC.PointProducts.DeleteOnSubmit(result);
            m_CosmeticDC.SubmitChanges();
        }


    }
}
